<?php 	
require_once ('checks.php');
if (isset($_GET['news_name']))	{
	require_once ('Database.php');
	require_once ('News.php');
	$db= new  DB();
	if (isset($_POST['go'])) {		
		if (!isset($_POST['newsvisible'])) {
			$flag=0;
		} else {
			$flag=1;
		}
		if (!isset($_GET['image'])){
			$_GET['image'] = $_SESSION['image'];
		}	
		$news= new News($_POST['news_name'],$_POST['news'],$_GET['image'],$_POST['order'],$flag);
		$news->setId($_SESSION['id']);
		
		$arr = $db->findName("news");
		$newsarr= sortBy($arr, "order" , "asc");	
		$n= count($newsarr);
		for ($i=0;$i<$n;$i++){
			if ($newsarr[$i]->getId()==$news->getId()){
				$idx=$i;
				break;
			}
		}

			$newsarr = $db->reOrderObjArray($newsarr, $idx, $_POST['order']);
			reOrderNewsInDatabase($newsarr);
			$db->updateNews($news);



	} else {
		$news = $db->extractNews($_GET['news_name']);
		$_SESSION['id']=$news->getId();
		$_SESSION['image'] = $news->getImage();
	}

} else {
	echo " Something Wrong Happened - please go to homepage!";
	die();
}

?>

		<form method="post" action="?page=edit_news<?php 			if (isset($_GET['image'])){
																		echo "&image=".$_GET['image'];
																	}
																	echo "&news_name=".$_GET['news_name']; 
													?>">
			<table>
				<tr>
					<td><label for="art_name">Article Name : </label>
					</td>
					<td><select name="art_name" id="art_name">
							<?php
								printSelectArticles($news->getId());
							?>
					</select>
					</td>
				</tr>
				<tr>
					<td><label for="newsvisible">Visible : </label>
					</td>
					<td><input type="checkbox" name="newsvisible" id="newsvisible" <?php if ($news->getVisible() == 1) { echo "checked";} ?>  >

					</td>
				</tr>
				<tr>
					<td><label for="order">Order : </label>
					</td>
					<td><input type="text" name="order" id="order" size="2" value=" <?php echo $news->getOrder(); ?> ">
					</td>
				</tr>
				<tr>
						<td>
							<label for="news_name">News Name : </label>
						</td>
						<td colspan="2">	
							<input type="text" name="news_name" id="news_name" value="<?php echo $news->getName()?>">
							
						</td>
				</tr>
				<tr>
					<td><label for="news_image"> Image : </label>
					</td>
					<td>
					<br/><img src='images/gallery/<?php 	
														if (isset($_GET['image'])) {					
															echo $_GET['image'];
															
														} else {
															echo $news->getImage();
														}
													?>' width='300' height='150'>
																						
						<br/><input type='button' value='Choose Image' onclick="location.href='?page=news_image&references=edit_news&news_name=<?php echo $_GET['news_name']?>'">
					</td>
				</tr>
				<tr>
						<td style="vertical-align:top;">
							<label for="news" >News : </label>
						</td>
						<td colspan="2">	
							<textarea name="news" id="news" cols="50" rows="10" >
								<?php 
	 								echo $news->getText();
								?>
							</textarea>
					</td>
				</tr>
				<tr>
					<td>
						
					</td>
					<td ><input type="submit" value="Save" name="go">
					</td>				
				</tr>			
			</table>
		</form>

